package com.bear.fastrepair.mvvm.ui.manager

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.bear.fastrepair.entity.OrderInfoPage2
import com.bear.fastrepair.mvvm.data.Result
import com.bear.fastrepair.mvvm.data.login.ManagerOperateResult
import com.bear.fastrepair.mvvm.data.manager.ManagerRepository
import com.bear.fastrepair.utils.launch

class ManagerViewModel(private val managerRepo: ManagerRepository) : ViewModel() {
    private val _addManager = MutableLiveData<ManagerOperateResult>()
    val addManagerResult: LiveData<ManagerOperateResult> = _addManager

    private val _getOrderInfoPage = MutableLiveData<OrderInfoPage2>()
    val getOrderInfoPage: LiveData<OrderInfoPage2> = _getOrderInfoPage

    fun addManagerUser(
        account: String,
        avatar: String,
        password: String,
        regionCode: String,
        regionText: String = "",
        roleCode: String,
        tel: String,
        userName: String,
        token: String = ""
    ) {
        launch {
            when (val result = managerRepo.addManagerUser(
                account,
                avatar,
                password,
                regionCode,
                regionText,
                roleCode,
                tel,
                userName,
                token
            )) {
                is Result.Success -> _addManager.value =
                    ManagerOperateResult(success = "管理员 $userName 已成功添加")
                is Result.Error -> _addManager.value =
                    ManagerOperateResult(error = result.data?.msg)
            }
        }
    }

    fun getOrderInfoPage(
        current: Int,
        size: Int,
        clientName: String? = null,
        clientTel: String? = null,
        employeeId: String? = null,
        orderNum: String? = null,
        orderStatus: Int? = null,
        orderTotalityStatus: Int? = null,
        regionCode: String? = null,
        token: String? = null
    ) {
        launch {
            when (val result = managerRepo.getOrderInfoPage(
                current,
                size,
                clientName,
                clientTel,
                employeeId,
                orderNum,
                orderStatus,
                orderTotalityStatus,
                regionCode,
                token
            )) {
                is Result.Success -> _getOrderInfoPage.value = result.data
                is Result.Error -> _getOrderInfoPage.value = result.data!!
            }
        }
    }
}